package com.atguigu.java8.lambdaDemo;

import org.junit.Test;


import java.util.*;

/**
 * @author Xiaohong X Gou
 * @createTime 2022-10-11 09:50
 * @description
 */
public class TestLambda {
    @Test
    public void test1(){
        Comparator<Integer> com =new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(01,02);
            }
        };
        TreeSet<Integer> ts=new TreeSet<>(com);
    }
    @Test
    public void test2(){
        Comparator<Integer> com=(x,y)->Integer.compare(x,y);
        TreeSet<Integer> ts =new TreeSet<>(com);
    }

    List<Employee> employees = Arrays.asList(
            new Employee(1,"张三",18,9999.99),
            new Employee(2,"李四",38,5555.99),
            new Employee(3,"王五",50,6666.66),
            new Employee(4,"赵六",16,3333.33),
            new Employee(5,"田七",8,7777.77)
    );

    @Test
    public void test3(){
        List<Employee> list = filterEmployees(employees);
        for (Employee emp :
                list) {
            System.out.println(emp);
        }

    }
    public List<Employee> filterEmployees(List<Employee> list){
        List<Employee> emps = new ArrayList<>();

        for (Employee emp: list) {
            if (emp.getAge()>=35){
                emps.add(emp);
            }
        }
        return emps;
    }

    public List<Employee> filterEmployees2(List<Employee> list){
        List<Employee> emps = new ArrayList<>();

        for (Employee emp: list) {
            if (emp.getSalary()>=5000){
                emps.add(emp);
            }
        }
        return emps;
    }
    @Test
    public void test4(){
        List<Employee> list = filterEmployee(employees, new FilterEmployeeByAge());
        for (Employee employee :
                list) {
            System.out.println(employee);
        }
        System.out.println("..........................");
        List<Employee> list2 = filterEmployee(employees, new FilterEmployeeBySalary());
        for (Employee emp: list2){
            System.out.println(emp);
        }
    }

    public List<Employee> filterEmployee(List<Employee> list,MyPredicate<Employee> myPredicate){
        List<Employee> emps=new ArrayList<>();
        for (Employee employee :
                list) {
            if (myPredicate.test(employee)) {
                emps.add(employee);
            }
        }
        return emps;
    }

    @Test
    public void test5(){
        List<Employee> list = filterEmployee(employees, new MyPredicate<Employee>() {
            @Override
            public boolean test(Employee t) {
                return t.getSalary() <= 5000;
            }
        });
        for (Employee emp:list){
            System.out.println(emp);
        }
    }
    @Test
    public void test6(){
        List<Employee> list = filterEmployee(employees, (e) -> e.getSalary() <= 5000);
        list.forEach(System.out::println);

    }
    @Test
    public void test7(){
        employees.stream()
                .filter(e->e.getSalary()>=5000)
                .limit(2)
                .forEach(System.out::println);

        System.out.println("....................");
        employees.stream()
                .map(Employee::getName)
                .forEach(System.out::println);
    }
}
